﻿using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Reflection;
using System.Text;
using System.Web;
using System.Web.Mvc;
using System.Xml;
using WeiSha.Core.Parameters.Authorization;

namespace WeiSha.Core
{
	// Token: 0x0200002F RID: 47
	public class LicenseView : IView
	{
		// Token: 0x060001B9 RID: 441 RVA: 0x0000C25C File Offset: 0x0000A45C
		public void Render(ViewContext viewContext, TextWriter writer)
		{
			string @string = Request.QueryString["action"].String;
			bool flag = @string == "refresh";
			if (flag)
			{
				License.Value.Init();
			}
			HttpContextBase httpContext = viewContext.HttpContext;
			StringBuilder stringBuilder = new StringBuilder();
			try
			{
				XmlDocument xmlDocument = this.Index();
				StringWriter stringWriter = new StringWriter(stringBuilder);
				XmlTextWriter xmlTextWriter = null;
				try
				{
					httpContext.Response.ContentType = "text/xml";
					xmlTextWriter = new XmlTextWriter(stringWriter);
					xmlTextWriter.Formatting = Formatting.Indented;
					xmlTextWriter.Indentation = 1;
					xmlTextWriter.IndentChar = '\t';
					xmlDocument.WriteTo(xmlTextWriter);
				}
				finally
				{
					bool flag2 = xmlTextWriter != null;
					if (flag2)
					{
						xmlTextWriter.Close();
					}
				}
				writer.Write(stringBuilder.ToString());
			}
			catch (Exception ex)
			{
				httpContext.Response.ContentType = "text/plain";
				stringBuilder.Append("Error:");
				stringBuilder.Append(ex.Message);
				writer.Write(stringBuilder.ToString());
			}
		}

		// Token: 0x060001BA RID: 442 RVA: 0x0000C380 File Offset: 0x0000A580
		public XmlDocument Index()
		{
			XmlDocument xmlDocument = new XmlDocument();
			XmlNode xmlNode = xmlDocument.CreateXmlDeclaration("1.0", "utf-8", "yes");
			xmlDocument.AppendChild(xmlNode);
			XmlNode xmlNode2 = xmlDocument.CreateProcessingInstruction("xml-stylesheet", "type=\"text/xsl\" href=\"/Utilities/License/Index.xsl\"");
			xmlDocument.AppendChild(xmlNode2);
			XmlElement xmlElement = xmlDocument.CreateElement("Platform");
			xmlElement.AppendChild(this._createDetails(xmlDocument));
			XmlElement xmlElement2 = xmlDocument.CreateElement("License");
			License value = License.Value;
			Type type = value.GetType();
			foreach (PropertyInfo propertyInfo in type.GetProperties())
			{
				object value2 = type.GetProperty(propertyInfo.Name).GetValue(value, null);
				Type underlyingType = Nullable.GetUnderlyingType(propertyInfo.PropertyType);
				string text = ((underlyingType != null) ? underlyingType.Name : propertyInfo.PropertyType.Name);
				bool flag = text == "VersionLimit" || text == "License";
				if (!flag)
				{
					XmlElement xmlElement3 = xmlDocument.CreateElement(propertyInfo.Name);
					bool flag2 = "LimitItems".Equals(propertyInfo.Name, StringComparison.CurrentCultureIgnoreCase);
					if (flag2)
					{
						xmlElement3.SetAttribute("type", "IDictionary[string, int]");
						IDictionary<string, int> dictionary = (IDictionary<string, int>)value2;
						bool flag3 = dictionary != null;
						if (flag3)
						{
							IDictionary<string, int> dictionary2 = Edition.DataItems(value.EdtionLevel);
							List<string> list = new List<string>(dictionary.Keys);
							List<string> list2 = new List<string>(dictionary2.Keys);
							for (int j = 0; j < dictionary.Count; j++)
							{
								string text2 = list[j];
								XmlElement xmlElement4 = xmlDocument.CreateElement("item");
								xmlElement4.SetAttribute("name", text2);
								xmlElement4.SetAttribute("entity", list2[j]);
								xmlElement4.InnerText = dictionary[text2].ToString();
								xmlElement3.AppendChild(xmlElement4);
							}
						}
					}
					else
					{
						xmlElement3.SetAttribute("type", text);
						bool flag4 = text == "String" || text == "String[]";
						if (flag4)
						{
							xmlElement3.AppendChild(xmlDocument.CreateCDataSection(this._to_property(text, value2)));
						}
						else
						{
							xmlElement3.InnerText = this._to_property(text, value2);
						}
					}
					xmlElement2.AppendChild(xmlElement3);
				}
			}
			xmlElement.AppendChild(xmlElement2);
			XmlElement xmlElement5 = xmlDocument.CreateElement("DataDetails");
			Dictionary<string, int> dictionary3 = Database.TableCount();
			long num = 0L;
			bool flag5 = dictionary3 != null;
			if (flag5)
			{
				foreach (KeyValuePair<string, int> keyValuePair in dictionary3)
				{
					bool flag6 = keyValuePair.Value <= 0;
					if (!flag6)
					{
						XmlElement xmlElement6 = xmlDocument.CreateElement(keyValuePair.Key);
						xmlElement6.InnerText = keyValuePair.Value.ToString();
						num += (long)keyValuePair.Value;
						xmlElement5.AppendChild(xmlElement6);
					}
				}
			}
			xmlElement5.SetAttribute("dbType", Database.DbType);
			xmlElement5.SetAttribute("tatol", num.ToString());
			xmlElement5.SetAttribute("initialDate", Database.InitialDate.ToString("yyyy-MM-dd HH:mm:ss"));
			xmlElement.AppendChild(xmlElement5);
			XmlElement xmlElement7 = xmlDocument.CreateElement("Activationcode");
			xmlElement7.AppendChild(this._createActionCode(xmlDocument, "CPU", Server.CPU_ID, Activationcode.CodeForCPU));
			xmlElement7.AppendChild(this._createActionCode(xmlDocument, "HardDisk", Server.HardDiskID, Activationcode.CodeForHardDisk));
			xmlElement7.AppendChild(this._createActionCode(xmlDocument, "IP", Server.IP + ":" + Server.Port, Activationcode.CodeForIP));
			xmlElement7.AppendChild(this._createActionCode(xmlDocument, "Domain", Server.Domain + ":" + Server.Port, Activationcode.CodeForDomain));
			xmlElement7.AppendChild(this._createActionCode(xmlDocument, "Root", Server.MainName + ":" + Server.Port, Activationcode.CodeForRoot));
			xmlElement.AppendChild(xmlElement7);
			DataTable dataTable = Request.EditionsChinese();
			XmlElement xmlElement8 = xmlDocument.CreateElement(dataTable.TableName);
			for (int k = 0; k < dataTable.Rows.Count; k++)
			{
				bool flag7 = "False".Equals(dataTable.Rows[k]["启用"].ToString(), StringComparison.OrdinalIgnoreCase);
				if (!flag7)
				{
					XmlElement xmlElement9 = xmlDocument.CreateElement("level");
					XmlElement xmlElement10 = xmlDocument.CreateElement("index");
					xmlElement10.InnerText = (k + 1).ToString();
					xmlElement9.AppendChild(xmlElement10);
					for (int l = 0; l < dataTable.Columns.Count; l++)
					{
						bool flag8 = "启用".Equals(dataTable.Columns[l].ColumnName, StringComparison.OrdinalIgnoreCase);
						if (!flag8)
						{
							XmlElement xmlElement11 = xmlDocument.CreateElement(dataTable.Columns[l].ColumnName);
							xmlElement11.InnerText = dataTable.Rows[k][l].ToString();
							xmlElement9.AppendChild(xmlElement11);
						}
					}
					xmlElement8.AppendChild(xmlElement9);
				}
			}
			xmlElement.AppendChild(xmlElement8);
			xmlDocument.AppendChild(xmlElement);
			return xmlDocument;
		}

		// Token: 0x060001BB RID: 443 RVA: 0x0000C960 File Offset: 0x0000AB60
		protected XmlElement _createDetails(XmlDocument xmldoc)
		{
			XmlElement xmlElement = xmldoc.CreateElement("AppDetails");
			foreach (KeyValuePair<string, string> keyValuePair in App.Details)
			{
				XmlElement xmlElement2 = xmldoc.CreateElement("item");
				xmlElement2.SetAttribute("remark", keyValuePair.Key);
				xmlElement2.InnerText = keyValuePair.Value;
				xmlElement.AppendChild(xmlElement2);
			}
			return xmlElement;
		}

		// Token: 0x060001BC RID: 444 RVA: 0x0000C9F8 File Offset: 0x0000ABF8
		private string _to_property(string typename, object value)
		{
			string text;
			if (!(typename == "DateTime"))
			{
				if (!(typename == "String"))
				{
					if (!(typename == "String[]"))
					{
						if (!(typename == "TimeSpan"))
						{
							if (!(typename == "Boolean"))
							{
								if (!(typename == "Int32"))
								{
									text = ((value == null) ? "" : value.ToString());
								}
								else
								{
									text = ((value == null) ? "" : value.ToString());
								}
							}
							else
							{
								text = value.ToString().ToLower();
							}
						}
						else
						{
							TimeSpan timeSpan = (TimeSpan)value;
							int num = (int)timeSpan.TotalDays;
							int num2 = num / 365;
							int num3 = num % 365;
							int num4 = num3 / 30;
							int num5 = num3 % 30;
							StringBuilder stringBuilder = new StringBuilder();
							bool flag = num2 > 0;
							if (flag)
							{
								stringBuilder.Append(num2.ToString() + "年");
							}
							bool flag2 = num4 > 0;
							if (flag2)
							{
								stringBuilder.Append(num4.ToString() + "月");
							}
							bool flag3 = num5 > 0;
							if (flag3)
							{
								stringBuilder.Append(num5.ToString() + "天");
							}
							bool flag4 = timeSpan.Hours > 0;
							if (flag4)
							{
								stringBuilder.Append(timeSpan.Hours.ToString() + "小时");
							}
							bool flag5 = stringBuilder.Length == 0;
							if (flag5)
							{
								bool flag6 = timeSpan.Minutes > 0;
								if (flag6)
								{
									stringBuilder.Append(timeSpan.Minutes.ToString() + "分钟");
								}
								bool flag7 = timeSpan.Seconds > 0;
								if (flag7)
								{
									stringBuilder.Append(timeSpan.Seconds.ToString() + "秒");
								}
							}
							text = stringBuilder.ToString();
						}
					}
					else
					{
						text = string.Join("、", (string[])value);
					}
				}
				else
				{
					text = ((value == null) ? "" : value.ToString().Replace("\"", "'"));
				}
			}
			else
			{
				DateTime dateTime = DateTime.Now;
				bool flag8 = value != null;
				if (flag8)
				{
					dateTime = Convert.ToDateTime(value);
				}
				bool flag9 = dateTime.Hour > 0 && dateTime.Minute > 0;
				if (flag9)
				{
					text = dateTime.ToString("yyyy年MM月dd日 HH:mm:ss");
				}
				else
				{
					text = dateTime.ToString("yyyy年MM月dd日");
				}
			}
			return text;
		}

		// Token: 0x060001BD RID: 445 RVA: 0x0000CC9C File Offset: 0x0000AE9C
		private XmlElement _createActionCode(XmlDocument doc, string name, string attr, string text)
		{
			XmlElement xmlElement = doc.CreateElement(name);
			xmlElement.SetAttribute("value", attr);
			xmlElement.InnerText = text;
			return xmlElement;
		}
	}
}
